﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CTOOLS
{
    public class DsoframerRegister
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static bool RegisteDsoframer()
        {
            bool bReturn = false;
            try
            {
                string ExePath=System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                string fileFullPath = ExePath + "dsoframer.ocx";// string.Format("{0}\\dsoframer.ocx", System.Environment.CurrentDirectory);
                // string systemDrive = System.Environment.GetEnvironmentVariable("systemdrive");//获取系统所在的盘符
             
                if (File.Exists(fileFullPath))  //|| String.IsNullOrEmpty(systemDrive))
                {
                    bool isRegisted = IsRegistered("CLSID\\{00460182-9E5E-11d5-B7C8-B8269041DD57}\\InprocServer32\\");
                    if (isRegisted)
                    {
                        bReturn = true;
                    }
                    else
                    {
                        //string windowsPath = string.Empty;

                        //if (GetOSBitCount() == "64")
                        //    windowsPath = string.Format("{0}\\Windows\\SysWOW64", systemDrive);
                        //else
                        //    windowsPath = string.Format("{0}\\Windows\\System32", systemDrive);

                        //if (!Directory.Exists(windowsPath))
                        //    return;


                        //File.Copy(fileFullPath, string.Format("{0}\\dsoframer.ocx", windowsPath), true);

                        // bReturn = Registe(string.Format("{0}\\dsoframer.ocx", fileFullPath));//windowsPath
                        bReturn = Registe(fileFullPath);

                    }

                }




            }
            catch (Exception ex)
            {
                log.Error("注册Dsoframer.ocx失败!", ex);
            }
            return bReturn;
        }

        //注册dsoframer.ocx
        private static bool Registe(string fileFullName)
        {
            bool bReturn = false;
            try
            {
                // System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32",  "/s " + fileFullName);//注册完毕不显示是否成功的提示

                //// System.Diagnostics.Process p = new System.Diagnostics.Process();


                // //  new Command().Execute(string.Format(@"Regsvr32.exe {0}", fileFullName + " /s"));                                                                                                 //System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName);//注册完毕显示是否成功的提示
                // if (p != null && p.HasExited)
                // {
                //     //p.StartInfo.FileName = "Regsvr32.exe";
                //     //p.StartInfo.Arguments = "/s " + fileFullName;
                //     //p.Start();

                //     Int32 exitCode = p.ExitCode;
                //     if (exitCode == 0)
                //         bReturn = true;
                // }

                RegistryKey regKey = null;
                RegistryKey reguKey = null;
                RegistryKey reguKeyControl = null;
                RegistryKey reguKeyGetSet = null;
                //RegistryKey regKey2 = null;
                if (Environment.Is64BitOperatingSystem)
                {
                    regKey = Registry.ClassesRoot.OpenSubKey("Wow6432Node\\CLSID", true);

                    reguKey = regKey.CreateSubKey("{00460182-9E5E-11d5-B7C8-B8269041DD57}");
                    reguKey.SetValue("", "DSO Framer Control Object");
                    //1
                    reguKeyControl = reguKey.CreateSubKey("Control");
                    //2
                    reguKeyControl = reguKey.CreateSubKey("InprocServer32");
                    reguKeyControl.SetValue("", fileFullName);
                    reguKeyControl.SetValue("ThreadingModel", "Apartment");
                    //3
                    reguKeyControl = reguKey.CreateSubKey("MiscStatus");
                    reguKeyControl.SetValue("", "131473");
                    //4
                    reguKeyControl = reguKey.CreateSubKey("ProgID");
                    reguKeyControl.SetValue("", "DSOFramer.FramerControl");
                    //5
                    reguKeyControl = reguKey.CreateSubKey("ToolboxBitmap32");
                    //reguKeyControl.SetValue("", @"D:\开发工具\DsoFramer\dsoframer.ocx,102");
                    reguKeyControl.SetValue("", fileFullName + ",102");
                    //6
                    reguKeyControl = reguKey.CreateSubKey("TypeLib");
                    reguKeyControl.SetValue("", "{00460180-9E5E-11d5-B7C8-B8269041DD57}");
                    //7
                    reguKeyControl = reguKey.CreateSubKey("Version");
                    reguKeyControl.SetValue("", "1.3");
                    //8
                    reguKeyControl = reguKey.CreateSubKey("DataFormats");
                    reguKeyGetSet = reguKeyControl.CreateSubKey("GetSet");
                    reguKeyControl = reguKeyGetSet.CreateSubKey("0");
                    reguKeyControl.SetValue("", "3,1,32,1");

                    reguKey.Close();
                    reguKey.Close();
                    reguKeyControl.Close();
                    reguKeyGetSet.Close();
                }
                else
                {
                    regKey = Registry.ClassesRoot.OpenSubKey("CLSID", true);

                    reguKey = regKey.CreateSubKey("{00460182-9E5E-11d5-B7C8-B8269041DD57}");
                    reguKey.SetValue("", "DSO Framer Control Object");
                    //1
                    reguKeyControl = reguKey.CreateSubKey("Control");
                    //2
                    reguKeyControl = reguKey.CreateSubKey("InprocServer32");
                    reguKeyControl.SetValue("", fileFullName);
                    reguKeyControl.SetValue("ThreadingModel", "Apartment");
                    //3
                    reguKeyControl = reguKey.CreateSubKey("MiscStatus");
                    reguKeyControl.SetValue("", "131473");
                    //4
                    reguKeyControl = reguKey.CreateSubKey("ProgID");
                    reguKeyControl.SetValue("", "DSOFramer.FramerControl");
                    //5
                    reguKeyControl = reguKey.CreateSubKey("ToolboxBitmap32");
                    reguKeyControl.SetValue("", fileFullName+",102");
                    //6
                    reguKeyControl = reguKey.CreateSubKey("TypeLib");
                    reguKeyControl.SetValue("", "{00460180-9E5E-11d5-B7C8-B8269041DD57}");
                    //7
                    reguKeyControl = reguKey.CreateSubKey("Version");
                    reguKeyControl.SetValue("", "1.3");
                    //8
                    reguKeyControl = reguKey.CreateSubKey("DataFormats");
                    reguKeyGetSet = reguKeyControl.CreateSubKey("GetSet");
                    reguKeyControl = reguKeyGetSet.CreateSubKey("0");
                    reguKeyControl.SetValue("", "3,1,32,1");

                    reguKey.Close();
                    reguKey.Close();
                    reguKeyControl.Close();
                    reguKeyGetSet.Close();
                }
                bReturn = true;



            }
            catch (Exception ex)
            {
                log.Error("注册Dsoframer.ocx时获取注册表失败!", ex);
            }
            return bReturn;    
        }

        //判断控件是否已经注册
        private static bool IsRegistered(string CLSID)
        {
            bool bReturn = false;
            try
            {
                if (!String.IsNullOrEmpty(CLSID))
                {
                    //String key = "CLSID\\{"+ CLSID+"}"; //String.Format(@"CLSID\{{{0}}}", CLSID);
                    RegistryKey regKey = null;
                   // if (Environment.Is64BitOperatingSystem)
                    regKey = Registry.ClassesRoot.OpenSubKey(CLSID);// "CLSID\\{00460182-9E5E-11d5-B7C8-B8269041DD57}\\");
                        //regKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64);
                   // else
                   //     regKey = Registry.ClassesRoot.OpenSubKey(CLSID);
                    //regKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
                    if (regKey != null)
                    {
                        string fp=regKey.GetValue("").ToString();
                       
                        if (File.Exists(fp))
                        {
                            bReturn = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("注册Dsoframer.ocx时获取注册表失败!", ex);
            }
            return bReturn;
            
        }

    }
}
